package com.dalongxin.work.page.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.andview.refreshview.recyclerview.BaseRecyclerAdapter;
import com.dalongxin.work.R;
import com.dalongxin.work.data.IndexHomeData;
import com.dalongxin.work.utils.Utils;

import java.util.List;

public class IndexHomeAdapter extends BaseRecyclerAdapter<IndexHomeAdapter.IndexHomeViewHolder> {

    private static final int VIEW_TYPE_HEAD = 1;
    private static final int VIEW_TYPE_LIST = 2;

    private Context context;
    private List<IndexHomeData> list;

    public IndexHomeAdapter(Context context, List<IndexHomeData> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getAdapterItemViewType(int position) {
        if(position == 0)
            return VIEW_TYPE_HEAD;
        return VIEW_TYPE_LIST;
    }

    @Override
    public IndexHomeViewHolder getViewHolder(View view) {
        return new IndexHomeViewHolder(view, true);
    }

    @Override
    public IndexHomeViewHolder onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) {
        View v = null;
        if(viewType == VIEW_TYPE_HEAD) {
            v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_index_home_top, parent, false);
            return new IndexHomeTopViewHolder(v, true);
        }else {
            v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_index_home_list, parent, false);
            return new IndexHomeListViewHolder(v, true);
        }
    }

    @Override
    public void onBindViewHolder(IndexHomeViewHolder holder, int position, boolean isItem) {
        final IndexHomeData item = list.get(position);
        int viewType = getAdapterItemViewType(position);
        if(viewType == VIEW_TYPE_HEAD) {
            IndexHomeTopViewHolder viewHolder = (IndexHomeTopViewHolder)holder;
            viewHolder.imageView.setBackgroundResource(item.getResId());
            ViewGroup.LayoutParams para = viewHolder.imageView.getLayoutParams();
            para.width = Utils.getWindowWidth(context);
            para.height = (para.width * 9) / 16;
            viewHolder.imageView.setLayoutParams(para);
        }else {
            IndexHomeListViewHolder viewHolder = (IndexHomeListViewHolder)holder;
            viewHolder.imageView.setBackgroundResource(item.getResId());
            viewHolder.titleText.setText(item.getTitle());
            viewHolder.descText.setVisibility(View.GONE);
        }
    }

    @Override
    public int getAdapterItemCount() {
        return list == null ? 0 : list.size();
    }

    public class IndexHomeViewHolder extends RecyclerView.ViewHolder {

        public IndexHomeViewHolder(View itemView, boolean isItem) {
            super(itemView);
        }
    }

    public class IndexHomeTopViewHolder extends IndexHomeViewHolder {
        private ImageView imageView;
        public IndexHomeTopViewHolder(View itemView, boolean isItem) {
            super(itemView, isItem);
            imageView = itemView.findViewById(R.id.index_home_top_iv);
        }
    }

    public class IndexHomeListViewHolder extends IndexHomeViewHolder {
        private ImageView imageView;
        private TextView titleText;
        private TextView descText;
        public IndexHomeListViewHolder(View itemView, boolean isItem) {
            super(itemView, isItem);
            imageView = itemView.findViewById(R.id.index_home_list_iv);
            titleText = itemView.findViewById(R.id.index_home_list_text_title);
            descText = itemView.findViewById(R.id.index_home_list_text_desc);
        }
    }
}
